package com.feib.soeasy.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * @title (#)MenuItem.java<br>
 * @description 程式選單<br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/7 create by Jimmy Liu<br>
 */
@Entity
@Table(name = "MENU_ITEM")
public class MenuItem extends AuditEntity {

    private static final long serialVersionUID = 5632456776155383664L;

    /**
     * PK in Table
     */
    private Long menuItemPk;

    /**
     * 程式代碼
     */
    private String menuId;

    /**
     * 上層程式選單
     */
    private MenuItem parentMenuItem;

    /**
     * 程式PAGE
     */
    private String actionPage;

    /**
     * 帳號啟用 <li>true: 啟用</li> <li>false: 停用</li>
     */
    private Boolean enabled;

    /**
     * 排序NO
     */
    private Integer orderNo;

    /**
     * 程式類別名稱
     */
    private String actionBean;

    /**
     * 程式中文名稱
     */
    private String actionName;

    /**
     * 使用者角色
     */
    private Set<Role> roles;
    
    /**
     * 學校是否可用
     */
    private Boolean school;
    
    /**
     * 管委會是否可用
     */
    private Boolean committee;

    /**
     * 是否被選取
     */
    private Boolean checked;

    /**
     * @return the menuItemPk
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "MENU_ITEM_PK", nullable = false)
    public Long getMenuItemPk() {
        return menuItemPk;
    }

    /**
     * @param menuItemPk the menuItemPk to set
     */
    public void setMenuItemPk(Long menuItemPk) {
        this.menuItemPk = menuItemPk;
    }

    /**
     * @return the actionBean
     */
    @Column(name = "ACTION_BAEN", length = 150)
    public String getActionBean() {
        return actionBean;
    }

    /**
     * @param actionBean the actionBean to set
     */
    public void setActionBean(String actionBean) {
        this.actionBean = actionBean;
    }

    /**
     * @return the actionName
     */
    @Column(name = "ACTION_NAME", length = 255)
    public String getActionName() {
        return actionName;
    }

    /**
     * @param actionName the actionName to set
     */
    public void setActionName(String actionName) {
        this.actionName = actionName;
    }

    @Column(name = "MENU_ID", length = 50)
    public String getMenuId() {
        return menuId;
    }

    public void setMenuId(String menuId) {
        this.menuId = menuId;
    }

    @ManyToOne
    @JoinColumn(name = "PARENT_MENU_ITEM_PK", nullable = true)
    public MenuItem getParentMenuItem() {
        return parentMenuItem;
    }

    public void setParentMenuItem(MenuItem parentMenuItem) {
        this.parentMenuItem = parentMenuItem;
    }

    @Column(name = "ACTION_PAGE", length = 250)
    public String getActionPage() {
        return actionPage;
    }

    public void setActionPage(String actionPage) {
        this.actionPage = actionPage;
    }

    @Column(name = "IS_ENABLED")
    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    @Column(name = "ORDER_NO")
    public Integer getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(Integer orderNo) {
        this.orderNo = orderNo;
    }

    /**
     * @return 傳回 roles。
     */
    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER)
    @JoinTable(name = "ROLE_MENU", joinColumns = @JoinColumn(name = "MENU_ITEM_PK"), inverseJoinColumns = @JoinColumn(name = "ROLE_PK"))
    public Set<Role> getRoles() {
        return roles;
    }

    
    /**
     * @return the school
     */
    @Column(name = "IS_SCHOOL")
    public Boolean getSchool() {
        return school;
    }

    
    /**
     * @param school the school to set
     */
    public void setSchool(Boolean school) {
        this.school = school;
    }

    
    /**
     * @return the committee
     */
    @Column(name = "IS_COMMITTEE")
    public Boolean getCommittee() {
        return committee;
    }

    
    /**
     * @param committee the committee to set
     */
    public void setCommittee(Boolean committee) {
        this.committee = committee;
    }

    /**
     * @param roles 要設定的 roles。
     */
    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    /**
     * @return 傳回 checked。
     */
    @Transient
    public Boolean getChecked() {
        return checked;
    }

    /**
     * @param checked 要設定的 checked。
     */
    public void setChecked(Boolean checked) {
        this.checked = checked;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((menuItemPk == null) ? 0 : menuItemPk.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MenuItem other = (MenuItem) obj;
        if (menuItemPk == null) {
            if (other.menuItemPk != null)
                return false;
        }
        else if (!menuItemPk.equals(other.menuItemPk))
            return false;
        return true;
    }

}
